/* eslint-disable no-unused-expressions */
/* eslint-disable eqeqeq */
import { message } from 'antd';
import {
  getListEmpData,
  getListCustData,
  getSuppListData,
  markData,
  empmarkData,
  suppmarkData,
  coidOnceCust,
  coidOnceEmp,
  coidOnceSupp,
  getCustDetail,
  getCustDetailLists,
  getEmpDetail,
  getSupplierDetail,
  getSuppDetailLists,
  getCustUpdateDetails,
  getCustUpdateDetailLists,
} from '@/services/onebilling';

export default {
  state: {
    expandForm: false,
    activeKey: null,
    tabStatus: '2', // 页签
    formFieldsValue: {},
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    proBaseList: {},
    proFbList: [],
    proListData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    dataSource: '1',
    custDetail: {}, // 向客户收费详情数据
    empDetail: {}, // 向雇员收费详情数据
    supplierDetail: {}, // 向供应商收费详情数据
    custEmployeeDetail: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    }, // 向客户收费的员工明细
    supplierEmployeeDetail: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    }, // 向供应商收费的员工明细

    updateDetail: {}, // 向客户收费修改详情
    creatorList: [], // 创建人列表
  },
  effects: {
    // 获取列表
    *getListOnceBill({ payload }, { call, put }) {
      let response = null;
      if (payload.activeKey == '1') {
        // 向雇员收费
        response = yield call(getListEmpData, payload.params, payload.dataSource);
      } else if (payload.activeKey == '2') {
        // 向客户收费
        response = yield call(getListCustData, payload.params);
      } else if (payload.activeKey == '3') {
        // 向供应商收费
        response = yield call(getSuppListData, payload.params);
      }
      const {
        pageArg: { currentPage, pageSize },
      } = payload.params;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning('查询列表失败');
      }
    },

    // 标记已收费提交
    *markModelData({ payload, callback }, { call }) {
      const response = yield call(markData, payload);
      callback && callback(response);
    },

    // 员工已收费提交
    *empModelData({ payload, callback }, { call }) {
      const response = yield call(empmarkData, payload);
      callback && callback(response);
    },

    // 供应商提交
    *suppModelData({ payload, callback }, { call }) {
      const response = yield call(suppmarkData, payload);
      callback && callback(response);
    },

    // 作废
    *toVoid({ payload, callback }, { call }) {
      let response = null;
      if (payload.activeKey == 2) {
        // eslint-disable-next-line no-param-reassign
        delete payload.activeKey;
        response = yield call(coidOnceCust, payload);
      } else if (payload.activeKey == 1) {
        // eslint-disable-next-line no-param-reassign
        delete payload.activeKey;
        response = yield call(coidOnceEmp, payload);
      } else if (payload.activeKey == 3) {
        // eslint-disable-next-line no-param-reassign
        delete payload.activeKey;
        response = yield call(coidOnceSupp, payload);
      }
      callback && callback(response);
    },

    // 向客户收费详情
    *custDetail({ payload }, { call, put }) {
      yield put({
        type: 'getCustDetailInfo',
        payload: {
          data: {},
        },
      });
      const response = yield call(getCustDetail, payload);

      if (response.success) {
        yield put({
          type: 'getCustDetailInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }
    },

    // 向雇员收费详情
    *empDetail({ payload }, { call, put }) {
      yield put({
        type: 'getEmpDetailInfo',
        payload: {
          data: {},
        },
      });
      const response = yield call(getEmpDetail, payload);

      if (response.success) {
        yield put({
          type: 'getEmpDetailInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }
    },

    // 向供应商收费详情
    *supplierDetail({ payload }, { call, put }) {
      yield put({
        type: 'getSupplierDetailInfo',
        payload: {
          data: {},
        },
      });
      const response = yield call(getSupplierDetail, payload);

      if (response.success) {
        yield put({
          type: 'getSupplierDetailInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }
    },

    // 向客户收费员工明细详情
    *getCustDetailList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getCustDetailListInfo',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getCustDetailLists, payload);
      if (response.success) {
        yield put({
          type: 'getCustDetailListInfo',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }
    },

    // 向供应商收费员工明细详情
    *getSuppDetailList({ payload }, { call, put }) {
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'gettSuppDetailListInfo',
        payload: {
          data: [],
          currentPage: 1,
          pageSize: 10,
        },
      });
      const response = yield call(getSuppDetailLists, payload);
      if (response.success) {
        yield put({
          type: 'gettSuppDetailListInfo',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }
    },

    // 向供应商收费详情
    *getCustUpdateDetail({ payload, callback }, { call, put }) {
      // yield put({
      //   type: 'getCustUpdateDetailInfo',
      //   payload: {
      //     data: {},
      //   },
      // });

      const response = yield call(getCustUpdateDetails, payload);

      // if (response.success) {
      //   yield put({
      //     type: 'getCustUpdateDetailInfo',
      //     payload: {
      //       data: response.data,
      //     },
      //   });
      // } else {
      //   message.warning(response.message || '获取详情信息失败');
      // }

      // 向商务合同下拉
      yield put({
        type: 'getContList',
        payload: {
          data: response.data,
        },
      });

      callback && callback(response);
    },

    // 向商务合同下拉
    *getContList({ payload }, { call, put }) {
      const response = yield call(getCustUpdateDetailLists, { custId: payload.data.custId });
      if (response.success) {
        yield put({
          type: 'getCustUpdateDetailInfo',
          payload: {
            data: payload.data,
            response,
          },
        });
      } else {
        message.warning(response.message || '获取详情信息失败');
      }

      // callback && callback(response);
    },
  },

  reducers: {
    // 列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 更新数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    // 向客户收费详情
    getCustDetailInfo(state, { payload }) {
      return {
        ...state,
        custDetail: payload.data,
      };
    },

    // 向雇员收费详情
    getEmpDetailInfo(state, { payload }) {
      return {
        ...state,
        empDetail: payload.data,
      };
    },

    // 向供应商收费详情
    getSupplierDetailInfo(state, { payload }) {
      return {
        ...state,
        supplierDetail: payload.data,
      };
    },

    // 向客户收费员工明细
    getCustDetailListInfo(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        // custEmployeeDetail: payload.data,
        custEmployeeDetail: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 向供应商收费员工明细
    gettSuppDetailListInfo(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        supplierEmployeeDetail: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },

    // 向客户收费修改详情
    getCustUpdateDetailInfo(state, { payload }) {
      return {
        ...state,
        updateDetail: payload.data,
      };
    },
  },
};
